﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class EditMenuControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        TextBox danname, danpic, danlink, danmiaoshu, txtID, txtORDER_ID, newurl;
        DropDownList classDropDownList;
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            InitControl();
            string strid = base.Page.Request["ID"];
            string action = base.Page.Request["action"];
            string type = base.Page.Request["type"];
            if (!base.Page.IsPostBack) {
                Bind();
                if (string.IsNullOrEmpty(strid)) { txtID.Text = System.Guid.NewGuid().ToString().Replace("-", "").ToUpper(); } else { 
                    txtID.Text = strid;
                    EluZSProvider.Menu menu = _EluZSDataProvider.GetMenuOne(strid);
                    classDropDownList.SelectedValue=menu.Parent_id;
                    danname.Text=menu.Menu_title;
                    danpic.Text=menu.Menu_pic;
                    danlink.Text=menu.Menu_url;
                    danmiaoshu.Text=menu.Menu_memo;
                    txtORDER_ID.Text = menu.Order_id;
                    newurl.Text = menu.Menu_url_new;
                }
            }
            if (!string.IsNullOrEmpty(action) && action == "del")
            {
                bool del;
                if (!string.IsNullOrEmpty(type))
                {
                    del = _EluZSDataProvider.DeleteXmByID("zs_menu", "PARENT_ID", strid);
                }
                else {
                    del = _EluZSDataProvider.DeleteXmByID("zs_menu", "id", strid);
                }
                if (del) { base.Page.Response.Redirect("Menu.aspx"); }
            }
            Button savebtn = (Button)FindControl("savebtn");
            savebtn.Click += new EventHandler(savebtn_Click);
        }
        protected void InitControl() {
            danname = (TextBox)FindControl("danname");
            danpic = (TextBox)FindControl("danpic");
            danlink = (TextBox)FindControl("danlink");
            danmiaoshu = (TextBox)FindControl("danmiaoshu");
            txtID = (TextBox)FindControl("txtID");
            txtORDER_ID = (TextBox)FindControl("txtORDER_ID");
            newurl = (TextBox)FindControl("newurl");
            classDropDownList = (DropDownList)FindControl("classDropDownList");
        }
        protected void Bind()
        {
            List<EluZSProvider.Menu> menu = _EluZSDataProvider.GetMenuTitle();
            EluZSProvider.Menu item = new EluZSProvider.Menu();
            item.Parent_id = "";
            item.Menu_title = "父菜单";
            menu.Insert(0, item);
            classDropDownList.DataSource = menu;
            classDropDownList.DataValueField = "id";
            classDropDownList.DataTextField = "Menu_title";
            classDropDownList.DataBind();
        }

        protected void savebtn_Click(object sender, EventArgs e)
        {
            string strid = base.Page.Request["ID"];
            EluZSProvider.Menu menu = new EluZSProvider.Menu();
            menu.Id = txtID.Text;
            menu.Parent_id = classDropDownList.SelectedValue;
            menu.Menu_title = danname.Text;
            menu.Menu_pic = danpic.Text;
            menu.Menu_url = danlink.Text;
            menu.Menu_memo = danmiaoshu.Text;
            menu.Menu_url_new = newurl.Text;
            menu.Is_delete = "0";
            if (string.IsNullOrEmpty(strid))
            {
                if (classDropDownList.SelectedValue == "")
                {
                    menu.Order_id = _EluZSDataProvider.GetSomeWhere("zs_Menu", "Count(order_id)+1", " Parent_Id is null ");
                }
                else {
                    menu.Order_id = _EluZSDataProvider.GetSomeStr("zs_Menu", "Count(order_id)+1", "Parent_Id", classDropDownList.SelectedValue);
                }
            }
            else {
                menu.Order_id = txtORDER_ID.Text;
            }
            bool save = _EluZSDataProvider.SaveMenu(strid, menu);
            if (save) { base.Page.Response.Redirect("Menu.aspx"); }
        }
        
    }
}


